package com.link.base.base.accnt.controller;

import com.link.base.base.accnt.model.AccntFollow;
import com.link.base.base.accnt.service.AccntFollowService;
import com.link.base.user.model.CoreUser;
import com.link.base.base.common.model.Attachment;
import com.link.base.base.common.service.AttachmentService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.util.UserUtil;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Copyright  hande Company. All Rights Reserved.
 * 客户跟进记录controller
 * @author  unknown
 * @date:   2018/11/7 9:27
 * @version 1.0
 */

@Controller
@RequestMapping("/link/accntFollow")
public class AccntFollowController extends BasicController<AccntFollow> {

    @Resource
    private AccntFollowService accntFollowService;
    @Resource
    private AttachmentService attachmentService;

    @Override
    public BasicService<AccntFollow> getBasicService() throws Exception {
        return accntFollowService;
    }

    /**
     * 新建一条记录
     * @param       entity
     * @return
     * @exception
     * @date        2018/11/7 9:30
     * @version     1.0
     */

    @RequestMapping(value = "/insert")
    @ResponseBody
    @Override
    public Map<String, Object> insert(@RequestBody AccntFollow entity, HttpSession session,
                                      HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            beforInsert(entity, request);
            beforUpsert(entity, request);
            CoreUser u = UserUtil.getUser();
            // 创建人id
            entity.setCrmCreatedBy(u.getId());
            // 创建人企点id
            entity.setCrmCreatedByOpenId(u.getQdUserOpenid());
            entity.setCorpid(u.getCorpid());
            LCLogger.info().withMessageKey("insert").withMessage("新建跟进信息" + entity.getName()).flush();
            accntFollowService.insert(entity);
            LCLogger.info().withMessageKey("insert").withMessage("新建跟进信息新建完成" + entity.getName()).flush();
            AccntFollow newEntry = accntFollowService.queryById(entity);
            result.put("result", newEntry);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     *APP音频文件上传
     * @param multiFile 获取的MultipartFile文件，参数为'file',
     * @param request
     * @param respone
     * @return
     */

    @RequestMapping(value = "/voiceFileUpload", method = RequestMethod.POST, produces = "application/json")
    public Map<String, Object> voiceFileUpload(@RequestParam("file") MultipartFile multiFile, //
                                               HttpServletRequest request, HttpServletResponse respone) {
        Long replyId = Long.parseLong(request.getParameter("replyId"));
        String timeLength = request.getParameter("timeLength");
        String attachmentName = request.getParameter("attachmentName");
        Map<String, Object> map = new HashMap<>(4);
        try {
            Attachment record = new Attachment();
            record.setModuleType("GuideFollowUp");
            record.setSource("app");
            record.setHeadId(replyId);
            record.setTimeLength(timeLength);
            record.setAttachmentName(attachmentName);
            attachmentService.voiceFileUpload(multiFile, record);
            Attachment attachment = attachmentService.queryVoiceAttachmentByHeadId(replyId);
            map.put("success", "true");
            map.put("attachment", attachment);
        } catch (Exception e) {
            LCLogger.withException(e);
            map.put("result", e.getMessage());
            map.put("success", false);
        }
        return map;
    }
    /**
     * 重写delete方法,将其改成post请求
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */

    @Override
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> deleteById(@RequestBody AccntFollow entity, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        return super.deleteById(entity, session, request, response);
    }
}
